<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width:300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend">商品</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="erp_goods_code"
        label="商品code"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="商品单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="spec"
        label="商品规格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="change_price"
        label="变更价格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="总差异金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="suppliser_info.name"
        label="供应商名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="created_at"
        label="创建时间"
        align="center"
      ></el-table-column>
      
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="handleEdit(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- 选择商品搜索dialog -->
    <storthDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></storthDialog>
    <!-- 详情dialog -->
    <hlinfoDialog :dialog="dialog" @upData="getData"></hlinfoDialog>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import Pagination from "@/components/pagination";
import { Changepriceindex } from "@/api/stockHeight/stockhei";
import StorthDialog from "@/components/stockReturnDialog/storthDialog";
import HlinfoDialog from "@/components/stockReturnDialog/hlinfoDialog";
export default {
  components: {
    pagination: Pagination,
    storthDialog: StorthDialog,
    hlinfoDialog: HlinfoDialog
  },
  data() {
    return {
      //dialog信息
      commoditydialog: {
        centerDialogVisible: false
      },
      commodityNameList: {
        name: "",
        code: ""
      },
      dialog: {
        centerDialogVisible: false,
        id: ""
      },
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      search: {
        searchCode: ""
      }
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        erp_goods_code_str: this.commodityNameList.code
      };
      Changepriceindex(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: ""
      };
      let arrName = [];
      let arrCode = [];
      data.forEach(res => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    // 导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Changeprice/exportindex?token=" +
            getToken() +
            "&erp_goods_code_str=" +
            this.commodityNameList.code;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //高退低进
    handleEdit(scope) {
      this.dialog.centerDialogVisible = true;
      this.dialog.id = scope.row.id;
    },
    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
